package l1j.opqlo.NewSystem;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.TimeZone;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.Thread.PcOtherThreadPool;
import l1j.server.server.utils.StreamUtil;

public class ChatLog {

	private static final String fileName =Config.LOG_LOCATION+"記錄/對話紀錄/對話";
	
	private class ChatLogTimer extends TimerTask {

		private ScheduledFuture<?> _timer;

		@Override
		public void run() {
			BufferedWriter out = null;
			try {
				if (ChatLog.this.Log.length()<=0) {
					return;
				}
				final TimeZone tz = TimeZone.getTimeZone(Config.TIME_ZONE);
				final Calendar cal = Calendar.getInstance(tz);
				final int year = cal.get(Calendar.YEAR) - 2000;
				final StringBuilder timer = new StringBuilder().append(year).append("-").append(cal.get(Calendar.MONTH) + 1).append("-").append(cal.get(Calendar.DATE));
				out = new BufferedWriter(new FileWriter(fileName + timer.toString() + ".txt", true));
				timer.delete(0, timer.length());
				StringBuilder Log2 = new StringBuilder(1024).append(Log);
				Log.delete(0, Log.length());
				out.write(Log2.toString());
				Log2.delete(0, Log2.length());
			} catch (final Exception e) {
				ChatLog.this._log.log(Level.WARNING, "對話紀錄計時異常，重新啟動");
				PcOtherThreadPool.get().cancel(this._timer, false);
				final ChatLogTimer restart = new ChatLogTimer();
				restart.start();
			} finally {
				StreamUtil.close(out);
			}
		}

		private void start() {
			final int timeMillis = 60000;
			this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this,
					timeMillis, timeMillis);
		}
	}

	private final Logger _log = Logger.getLogger(ChatLog.class.getName());

	private static ChatLog _instance;

	private static HashMap<Integer, String> Title = new HashMap<Integer, String>();

	

	public static ChatLog get() {

		if (_instance == null) {
			_instance = new ChatLog();
			Start();

		}
		return _instance;
	}

	private static void Start() {
		Title.put(new Integer(0), "	【一般】");
		Title.put(new Integer(1), "	【密語】");
		Title.put(new Integer(2), "	【大喊】");
		Title.put(new Integer(3), "	【全體】");
		Title.put(new Integer(4), "	【血盟】");
		Title.put(new Integer(11), "	【隊伍】");
		Title.put(new Integer(12), "	【交易】");
		Title.put(new Integer(13), "	【聯盟】");
		Title.put(new Integer(14), "	【聊天】");
		ChatLog.get().new ChatLogTimer().start();
	}

	private final StringBuffer Log = new StringBuffer(1024);

	public void ChatLogSave() throws IOException {
		if (this.Log.length()<=0) {
			return;
		}
		BufferedWriter out = null;
		try {
			if (ChatLog.this.Log.length()<=0) {
				return;
			}
			final TimeZone tz = TimeZone.getTimeZone(Config.TIME_ZONE);
			final Calendar cal = Calendar.getInstance(tz);
			final int year = cal.get(Calendar.YEAR) - 2000;
			final StringBuilder timer = new StringBuilder().append(year).append("-").append(cal.get(Calendar.MONTH) + 1).append("-").append(cal.get(Calendar.DATE));
			out = new BufferedWriter(new FileWriter(fileName + timer.toString() + ".txt", true));
			timer.delete(0, timer.length());
			StringBuilder Log2 = new StringBuilder(1024).append(Log);
			Log.delete(0, Log.length());
			out.write(Log2.toString());
			Log2.delete(0, Log2.length());
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage() ,e);
		} finally {
			StreamUtil.close(out);
		}

	}

	public void setLog(final StringBuilder ip, final int type, final StringBuilder s) {
		this.Log.append("IP	").append(ip).append("	").append(new Timestamp(System.currentTimeMillis())).append("	").append(Title.get(type)).append(s).append("\r\n");
	}

}
